package com.ljf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljf.annotation.HasPermission;
import com.ljf.dto.MemberPageDto;
import com.ljf.entity.Member;
import com.ljf.service.MemberService;
import com.ljf.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Validated
@RequestMapping("/member_management/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 分页
     * @param memberPageDto
     * @return
     */
    @HasPermission("member_management:member:list")
    @PostMapping("/queryPageByQo")
    public Result pageQuery(MemberPageDto memberPageDto){
        Page<Member> page = memberService.pageQuery(memberPageDto);
        return Result.success(page);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @HasPermission("member_management:member:delMember")
    @PostMapping("/delMember")
    public Result deleteMember(Long id) {
        memberService.deleteMember(id);
        return Result.success();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/queryMemberById")
    public Result queryMemberById(Long id) {
        Member member = memberService.queryMemberById(id);
        return Result.success(member);
    }

    /**
     * 根据用户名查询
     * @param phone
     * @return
     */
    @GetMapping("/queryMemberByPhone")
    public Result queryMemberByPhone(String phone) {
        Member member = memberService.queryMemberByPhone(phone);
        return Result.success(member);
    }

    /**
     * 新增会员
     * @param member
     * @return
     */
    @HasPermission("member_management:member:save")
    @PostMapping("/save")
    public Result save(Member member) {
        memberService.saveMember(member);
        return Result.success();
    }

    /**
     * 修改操作
     * @param member
     * @return
     */
    @HasPermission("member_management:member:update")
    @PostMapping("/update")
    public Result update(Member member){
        memberService.updateMember(member);
        return Result.success();
    }
}
